package com.hippy.erya.impl;

import com.google.common.collect.Lists;
import com.hippy.erya.common.DateUtil;
import com.hippy.erya.entity.*;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class HomePageServiceImpl {

    @Autowired
    private MessageServiceImpl messageService;

    @Autowired
    private CostingServiceImpl costingService;

    @Autowired
    private OrderDetailServiceImpl orderDetailService;

    @Autowired
    private RawMaterialDailyLossServiceImpl rawMaterialDailyLossService;


    public HomePageEntity showHomeDashboard() {
        HomePageEntity homePageEntity = new HomePageEntity();
        homePageEntity.setUnMessage(convertMessage(messageService.queryLatestMessage()));
        homePageEntity.setCostingEntities(convertCosting(costingService.queryLatestCosting()));
        List<OrderDetailDO> orderDetailDOS = orderDetailService.queryLatestOrderDetails();
        homePageEntity.setOrderEvaluateEntities(convertOrderEvaluateEntities(orderDetailDOS));
        homePageEntity.setHotDishs(convertHotDish(orderDetailDOS));
        homePageEntity.setRawMaterialLossList(rawMaterialDailyLossService.queryLastWeek());
        return homePageEntity;
    }

    private List<HotDishEntity> convertHotDish(List<OrderDetailDO> orderDetailDOS) {
        if (CollectionUtils.isEmpty(orderDetailDOS)) {
            return null;
        }
        Map<String, Integer> map = new HashMap<>();
        orderDetailDOS.forEach(orderDetailDO -> {
            map.put(orderDetailDO.getDishName(), map.getOrDefault(orderDetailDO.getDishName(), 0) + 1);
        });

        List<HotDishEntity> result = new ArrayList<>();
        map.forEach((k, v) -> {
            HotDishEntity hotDishEntity = new HotDishEntity();
            hotDishEntity.setName(k);
            hotDishEntity.setCount(v);
            result.add(hotDishEntity);
        });
        result.sort((o1, o2) -> o2.getCount() - o1.getCount());
        if (result.size() > 5) {
            return result.subList(0, 5);
        }
        return result;
    }


    private List<OrderEvaluateEntity> convertOrderEvaluateEntities(List<OrderDetailDO> orderDetailDOS) {
        if (CollectionUtils.isEmpty(orderDetailDOS)) {
            return null;
        }
        Map<String, OrderEvaluateEntity> map = new HashMap<>();
        orderDetailDOS.forEach(orderDetailDO -> {
            String date = DateUtil.format(orderDetailDO.getOrderCreateDate(), DateUtil.DATE_SHOW_FORMAT);
            OrderEvaluateEntity orderEvaluateEntity = map.computeIfAbsent(date, k -> new OrderEvaluateEntity());
            orderEvaluateEntity.setDate(date);
            double orderScore = orderDetailDO.getOrderScore();
            if (orderScore > 4) {
                orderEvaluateEntity.setGoodScoreCount(orderEvaluateEntity.getGoodScoreCount() + 1);
            } else {
                orderEvaluateEntity.setBadScoreCount(orderEvaluateEntity.getBadScoreCount() + 1);
                orderEvaluateEntity.getBadOrderContents().add(orderDetailDO.getDishName()+ ": " + orderDetailDO.getOrderEvaluate());
            }
        });
        return new ArrayList<>(map.values());
    }


    private List<LatestCostingEntity> convertCosting(List<CostingDO> costingDOS) {
        if (CollectionUtils.isEmpty(costingDOS)) {
            return null;
        }
        Map<String, LatestCostingEntity> map = new HashMap<>();
        costingDOS.forEach(costingDO -> {
            String date = DateUtil.format(costingDO.getCostingDate(), DateUtil.DATE_SHOW_FORMAT);
            LatestCostingEntity costingEntity = map.computeIfAbsent(date, k -> new LatestCostingEntity());
            double totalCost = costingDO.getOrderAllIncome() - costingDO.getAllProfit();
            costingEntity.setAllCost(costingEntity.getAllCost() + totalCost);
            costingEntity.setOrderAllIncome(costingEntity.getOrderAllIncome() + costingDO.getOrderAllIncome());
            costingEntity.setAllProfit(costingEntity.getAllProfit() + costingDO.getAllProfit());
            costingEntity.setDate(date);
        });
        return Lists.newArrayList(map.values());
    }


    private UnMessageEntity convertMessage(Pair<Integer, List<MessageDO>> pair) {
        int unReadCount = pair.getLeft();
        if (unReadCount == 0) {
            return null;
        }
        UnMessageEntity unMessageEntity = new UnMessageEntity();
        unMessageEntity.setCount(unReadCount);
        List<MessageDO> messageDOS = pair.getRight();
        if (!CollectionUtils.isEmpty(messageDOS)) {
            unMessageEntity.setContents(messageDOS.stream().map(m -> m.getMessage()).collect(Collectors.toList()));
        }
        return unMessageEntity;
    }




}
